package com.adminsystem.controller;

import com.adminsystem.common.aop.AutoLog;
import com.adminsystem.common.utils.JWTUtil;
import com.adminsystem.common.utils.StringUtils;
import com.adminsystem.entity.*;
import com.adminsystem.entity.custom.NoticePageBean;
//import com.adminsystem.entity.custom.R;
import com.adminsystem.service.SysNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.core.http.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys/notice")
@Api(tags = "系统公告")
public class SysNoticeController {

    @Autowired
    private SysNoticeService sysNoticeService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:notice:list')")
    public R list() {
        List<SysNotice> sysNoticeList = sysNoticeService.list(new LambdaQueryWrapper<SysNotice>().orderByDesc(SysNotice::getUsername).last("limit 0,5"));
        return R.success(sysNoticeList);
    }

    @PostMapping("/listAll")
    @PreAuthorize("hasAuthority('system:notice:list')")
    public R listAll(@RequestBody NoticePageBean noticePageBean) {
        String query = noticePageBean.getQuery().trim();
        String content = noticePageBean.getContent().trim();
        Page<SysNotice> pageResult = sysNoticeService.page(
                new Page<>(noticePageBean.getPageNum(),
                        noticePageBean.getPageSize()),
                new QueryWrapper<SysNotice>()
                        .like(StringUtils.isNotEmpty(query), "username", query)
                        .like(StringUtils.isNotEmpty(content), "content", content)
                        .orderByDesc("create_time"));
        List<SysNotice> roleList = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("noticeList", roleList);
        resultMap.put("total", pageResult.getTotal());
        return R.success(resultMap);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('system:notice:add')")
    @AutoLog("新增公告信息")
    public R add(@RequestBody SysNotice sysNotice) {
        Date d = new Date();
        sysNotice.setCreateTime(d);
        sysNotice.setUpdateTime(d);
        sysNotice.setUsername(JWTUtil.getCurrentUserName());
        sysNoticeService.save(sysNotice);
        return R.success();
    }

    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('system:notice:edit')")
    @AutoLog("编辑公告信息")
    public R edit(@RequestBody SysNotice sysNotice) {
        Date d = new Date();
        sysNotice.setUpdateTime(d);
        sysNoticeService.updateById(sysNotice);
        return R.success();
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('system:notice:delete')")
    @AutoLog("删除公告信息")
    public R delete(@RequestBody long[] ids) {
        List<Long> idList = Arrays.stream(ids).boxed().collect(Collectors.toList());
        sysNoticeService.removeByIds(idList);
        return R.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:notice:query')")
    @AutoLog("获得公告信息")
    public R findById(@PathVariable(value = "id") Long id) {
        SysNotice sysNotice = sysNoticeService.getById(id);
        return R.success(sysNotice);
    }
}
